package com.wen.task.baseTasks;

import com.wen.entry.Result;
import com.wen.moudle.Account;
import com.wen.moudle.Managers.FightManager;
import com.wen.moudle.Managers.ShopManager;
import com.wen.task.AbstractTask;
import com.wen.utils.ConsoleUtils;
import lombok.extern.slf4j.Slf4j;

// 自动打npc碎片竞技场---完成
@Slf4j
public class AutoNpcDebrisArenaTask extends AbstractTask {

    private ShopManager shopManager;
    private FightManager fightManager;


    public AutoNpcDebrisArenaTask(Account account) {
        super(account);
        fightManager = getFightManager();
        shopManager = getShopManager();
    }

    @Override
    public String getTitle() {
        return "一键挂机npc碎片竞技场";
    }

    @Override
    public void run() {
        log.info("开始挂机npc碎片竞技场");
        Integer id = shopManager.getCommodityId("英雄帖");

        System.out.println("英雄帖不足时是否需要自动购买英雄帖?[y/n]");
        boolean flag = ConsoleUtils.yesOrNo();

        int count = 0;
        while (true) {
            Result<String> result = fightManager.npcDebrisArena();
            if (result.isSuccess()) {
                // 只要能挑战成功，下一轮继续挑战
                log.info("竞技场第 {} 次报名成功", ++count);
                continue;
            }
            // 因为英雄帖不足，挑战失败
            if (flag && result.getErrorMessage().contains("英雄帖")) {
                // 购买英雄帖
                Result<String> purchase = shopManager.purchase(id, 1);

                if (purchase.isSuccess()) continue; // 购买成功，接着挑战
                // 英雄帖购买失败，退出
                log.error("{}", purchase.getErrorMessage());
                break;
            } else {
                // 达到上限退出
                log.error("{}", result.getErrorMessage());
                break;
            }
        }
        log.info("本次共进行了 {} 场npc碎片竞技场", count);
    }

}

